package cn.itsource.pet.controller;

import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.PageResult;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.domain.SearchMasterMsgAuditLog;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     *
     * @param searchMasterMsg 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        if (searchMasterMsg.getId() != null) {
            searchMasterMsgService.update(searchMasterMsg);
        } else {
            searchMasterMsgService.add(searchMasterMsg);
        }
        return new AjaxResult();

    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        searchMasterMsgService.delete(id);
        return new AjaxResult();
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        searchMasterMsgService.patchdel(ids);
        return new AjaxResult();
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id") Long id) {
        return searchMasterMsgService.findOne(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<SearchMasterMsg> findAll() {
        return searchMasterMsgService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageResult<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 发布接口
     *
     * @param searchMasterMsg 发布消息
     * @param request         请求头获取登录人消息
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        return searchMasterMsgService.publish(searchMasterMsg, request);
    }


    /**
     * 接口：分页查询或高级查询 状态为2 已完成寻主消息
     *
     * @param query
     * @return
     */
    @PostMapping("/finish")
    public PageResult<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.finish(query, request);
    }

    /**
     * 接口：分页查询或高级查询 状态为0 待审核
     *
     * @param query
     * @return
     */
    @PostMapping("/toAudit")
    public PageResult<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.toAudit(query, request);
    }

    /**
     * 接口：分页查询或高级查询 状态为3 寻主池寻主消息
     *
     * @param query
     * @return
     */
    @PostMapping("/toPool")
    public PageResult<SearchMasterMsg> toPool(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.toPool(query, request);
    }

    /**
     * 接口：分页查询或高级查询 状态为1 待待处理寻主消息
     *
     * @param query
     * @return
     */
    @PostMapping("/toHandle")
    public PageResult<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.toHandle(query, request);
    }

    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody SearchMasterMsgAuditLog log, HttpServletRequest request) {
        return searchMasterMsgService.audit(log,request);
    }

    @GetMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId){
        return searchMasterMsgService.reject(msgId);
    }

    /**
     * 接单
     * @param map
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody Map<String,String> map){
        return searchMasterMsgService.accept(map);
    }
}
